package cn.com.blueInfo.bpm.number.service.impl;

import cn.com.blueInfo.bpm.number.convert.NumberRuleComponentConvert;
import cn.com.blueInfo.bpm.number.entity.NumberRuleComponent;
import cn.com.blueInfo.bpm.number.repository.NumberRuleComponentRepository;
import cn.com.blueInfo.bpm.number.request.NumberRuleComponentDTO;
import cn.com.blueInfo.bpm.number.response.NumberRuleComponentVO;
import cn.com.blueInfo.bpm.number.service.NumberRuleComponentService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 编号规则组成配置表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberRuleComponentServiceImpl implements NumberRuleComponentService {

    @Autowired
    private NumberRuleComponentRepository numberRuleComponentRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private NumberRuleComponentConvert numberRuleComponentConvert;

    /**
     * 新增编号规则组成配置
     * @param numberRuleComponentDTO 编号规则组成配置参数
     */
    @Override
    public ResultInfo<Integer> addNumberRuleComponent(NumberRuleComponentDTO numberRuleComponentDTO) {
        NumberRuleComponent numberRuleComponent =
                numberRuleComponentConvert.dtoToEntity(numberRuleComponentDTO);
        numberRuleComponent.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberRuleComponentRepository.createNumberRuleComponent(numberRuleComponent);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则组成配置成功新增 {} 条").build();
    }

    /**
     * 批量新增编号规则组成配置
     * @param numberRuleComponentDTOList 编号规则组成配置参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddNumberRuleComponent(List<NumberRuleComponentDTO> numberRuleComponentDTOList) {
        List<NumberRuleComponent> numberRuleComponentList =
                numberRuleComponentConvert.dtoToEntity(numberRuleComponentDTOList);
        for (NumberRuleComponent numberRuleComponent : numberRuleComponentList)
            numberRuleComponent.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberRuleComponentRepository.batchCreateNumberRuleComponent(numberRuleComponentList);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则组成配置成功新增 {} 条").build();
    }

    /**
     * 删除编号规则组成配置
     * @param numberRuleComponentId 编号规则组成配置ID
     */
    @Override
    public ResultInfo<Integer> removeNumberRuleComponent(Long numberRuleComponentId) {
        Integer removeSize =
                numberRuleComponentRepository.deleteNumberRuleComponent(numberRuleComponentId);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则组成配置成功删除 {} 条").build();
    }

    /**
     * 批量删除编号规则组成配置
     * @param numberRuleComponentIdList 编号规则组成配置ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveNumberRuleComponent(List<Long> numberRuleComponentIdList) {
        Integer removeSize =
                numberRuleComponentRepository.batchDeleteNumberRuleComponent(numberRuleComponentIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则组成配置成功删除 {} 条").build();
    }

    /**
     * 更新部分编号规则组成配置
     * @param numberRuleComponentUpdatePartialDTO 编号规则组成配置参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberRuleComponentPartial(
            NumberRuleComponentDTO.UpdatePartialDTO numberRuleComponentUpdatePartialDTO) {
        NumberRuleComponent numberRuleComponent =
                numberRuleComponentConvert.dtoToEntity(numberRuleComponentUpdatePartialDTO);
        Integer modifySize =
                numberRuleComponentRepository.updateNumberRuleComponentPartial(numberRuleComponent);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则组成配置成功更新 {} 条").build();
    }

    /**
     * 更新全部编号规则组成配置
     * @param numberRuleComponentDTO 编号规则组成配置参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberRuleComponent(NumberRuleComponentDTO numberRuleComponentDTO) {
        NumberRuleComponent numberRuleComponent =
                numberRuleComponentConvert.dtoToEntity(numberRuleComponentDTO);
        Integer modifySize =
                numberRuleComponentRepository.updateNumberRuleComponent(numberRuleComponent);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则组成配置成功更新 {} 条").build();
    }

    /**
     * 查询一条编号规则组成配置
     * @param numberRuleComponentId 编号规则组成配置ID
     */
    @Override
    public ResultInfo<NumberRuleComponentVO> queryNumberRuleComponentById(Long numberRuleComponentId) {
        NumberRuleComponent numberRuleComponent =
                numberRuleComponentRepository.getNumberRuleComponentById(numberRuleComponentId);
        NumberRuleComponentVO numberRuleComponentVO =
                numberRuleComponentConvert.entityToVO(numberRuleComponent);
        return ResultInfo.<NumberRuleComponentVO>builder().data(numberRuleComponentVO).message("编号规则组成配置查询成功").build();
    }

    /**
     * 分页查询编号规则组成配置
     * @param numberRuleComponentQueryDTO 编号规则组成配置参数
     */
    @Override
    public ResultInfo<Page<NumberRuleComponentVO>> queryNumberRuleComponentPage(
            NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {
        Page<NumberRuleComponent> numberRuleComponentPage =
                numberRuleComponentRepository.getNumberRuleComponentPage(numberRuleComponentQueryDTO);
        Page<NumberRuleComponentVO> resultData =
                numberRuleComponentConvert.convertPage(numberRuleComponentPage);
        return ResultInfo.<Page<NumberRuleComponentVO>>builder()
                .data(resultData)
                .message("编号规则组成配置分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 编号规则组成配置 数量
     * @param numberRuleComponentQueryDTO 编号规则组成配置参数
     */
    @Override
    public ResultInfo<Long> queryNumberRuleComponentCount(
            NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {
        Long resultCount = numberRuleComponentRepository.getNumberRuleComponentCount(numberRuleComponentQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("编号规则组成配置数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 编号规则组成配置
     * @param numberRuleComponentQueryDTO 编号规则组成配置参数
     */
    @Override
    public ResultInfo<List<NumberRuleComponentVO>> queryNumberRuleComponentList(
            NumberRuleComponentDTO.QueryDTO numberRuleComponentQueryDTO) {
        List<NumberRuleComponent> numberRuleComponentList =
                numberRuleComponentRepository.getNumberRuleComponentList(numberRuleComponentQueryDTO);
        List<NumberRuleComponentVO> resultData =
                numberRuleComponentConvert.entityListToVoList(numberRuleComponentList);
        return ResultInfo.<List<NumberRuleComponentVO>>builder()
                .data(resultData)
                .message("编号规则组成配置分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
